<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
    <link href="framework/reset.css" rel="stylesheet">
    <link rel="stylesheet" href="css/userAddress.css">
    <script src="js/pageJump.js"></script>
    <script src="https://fastly.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
    <div class="wrapper" id="app">
        <!-- header部分 -->
        <header>
            <p>地址管理</p>
        </header>
        <!-- 地址列表部分 -->
        <ul class="addresslist">
            <li v-for="(address, index) in addressList" @click="choiceAddress(index)">
                <div class="addresslist-left">
                    <h3>{{address.name+(address.sex==0?"先生":"女士")+" "+address.phone}}</h3>
                    <p>{{address.address}}</p>
                </div>
                <div class="addresslist-right">
                    <i class="fa fa-edit" @click="jumpTo('editUserAddress.html?id='+address.addressId)"></i>
                    <i class="fa fa-remove" @click="removeUserAddress(address.addressId)"></i>
                </div>
            </li>
        </ul>
        <!-- 新增地址部分 -->
        <div class="addbtn" @click="jumpTo('addUserAddress.html')">
            <i class="fa fa-plus-circle"></i>
            <p>新增收货地址</p>
        </div>


        

    </div>
    <script>
        var param=getRequest()
        var path=getRequestPath()
        var app=new Vue({
            el:"#app",
            data:{
                
                addressList:[],
                pointsList:[]
            },
            methods:{
                getAddress:async function(){
                    axios.post(path+"/DeliveryAddressController/listDeliveryAddressByUserId",null,{
                        params:{
                            
                        },
                        headers:{
                            Authorization:this.getAuthorization()
                        }
                    }).then((respon)=>{
                        if(respon.data.code==200){           
                            this.addressList=respon.data.detail
                        }else if(respon.data.code==400&&respon.data.detail.errorCode==401){
                            jumpToLogin()
                        }
                    }).catch((error)=>{
                        console.log(error)
                    })
                },
                choiceAddress:function(index){
                    if(param.id!=null){
                        this.jumpTo("order.html?id="+param.id+"&daId="+index)
                    }
                },
                jumpTo:function(u){
                    window.location.href=u
                },
                getAuthorization:()=>{
                    
                    return window.localStorage.getItem("Auth")
                },
                removeUserAddress:function(addressId,index){
                    axios.post(path+"/DeliveryAddressController/removeDeliveryAddress",null,{
                        params:{
                            daId:addressId
                        },
                        headers:{
                            Authorization:this.getAuthorization()
                        }
                    }).then((respon)=>{
                        if(respon.data.code==200){           
                            this.addressList.splice(this.addressList.indexOf(index-1), 1)
                        }
                    }).catch((error)=>{
                        console.log(error)
                    })
                },
                
            },
            created() {
                this.getAddress()
                this.getPointsList()
            }
        })
    </script>
</body>

</html>